package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzdy.dataplatform.controller.api.DelArr;
import com.fzdy.dataplatform.controller.api.ModuleCallback;
import com.fzdy.dataplatform.controller.api.protocol.ProtocolListApi;
import com.fzdy.dataplatform.controller.api.protocol.UserKtvApi;
import com.fzdy.dataplatform.controller.api.sysLog.LogStringUtil;
import com.fzdy.dataplatform.controller.api.sysLog.SysLogApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.Check;
import com.fzdy.dataplatform.core.controller.BaseController;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.entity.KTV;
import com.fzdy.dataplatform.entity.KTVGroup;
import com.fzdy.dataplatform.entity.User;
import com.fzdy.dataplatform.entity.UserKtv;
import com.fzdy.dataplatform.service.KTVGroupService;
import com.fzdy.dataplatform.service.KTVService;
import com.fzdy.dataplatform.service.UserKtvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/sysmanage/protocol")
public class ProtocolController extends BaseController {

    @Autowired
    private KTVGroupService ktvGroupService;
    @Autowired
    private UserKtvService userKtvService;
    @Autowired
    private KTVService ktvService;

    @RequestMapping(value = "/list")
    public String list(@RequestBody Page<KTVGroup> page, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        KTVGroup ktvGroup = new KTVGroup();
        ktvGroup.setName(page.getName());
        if (!webUtilify.isAdmin(request)) {
            ktvGroup.setUserid(webUtilify.LoginUserinfo(request).getId());
        } else {
            if (!Check.checkStringNull(page.getUname())) {
                User searchUser = new User();
                searchUser.setName(page.getUname());
                ktvGroup.setUser(searchUser);
            }
        }
        ktvGroupService.getPageByExampleLike(ktvGroup, page);
        ajaxCallback = new AjaxCallback(ktvGroupdbtoapi(page.getList(), webUtilify.isAdmin(request)), page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/getktvlist")
    public String getktvlist(HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        Integer uid=null;
        if (!webUtilify.isAdmin(request)){
            uid=webUtilify.LoginUserinfo(request).getId();
        }
        List<KTV> list = ktvService.getList(uid);
        ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "查询成功");
        ajaxCallback.setList(ProtocoldictionaryController.ktvdbtoapi(list));
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/edit")
    public String edit(@RequestBody ProtocolListApi api, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        SysLogApi log_sysLogApi = new SysLogApi(LogStringUtil.COMMUNICATIONPROTOCOL, LogStringUtil.SUCCESS,
                webUtilify.LoginUserinfo(request).getId());
        JSONObject log_content = new JSONObject();
        //添加
        if (api.getId() == null) {
            log_sysLogApi.setOperation(LogStringUtil.ADD);
            KTVGroup ktvGroup = new KTVGroup();
            ktvGroup.setUserid(webUtilify.LoginUserinfo(request).getId());
            ktvGroup.setName(api.getName());
            ktvGroup.setMemo(api.getMemo());
            log_content.put("私有协议", JSON.toJSONString(api));
            ktvGroupService.insertT(ktvGroup, api.getKtvlist());
            ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "添加成功");
        }
        //修改
        else {
            log_sysLogApi.setOperation(LogStringUtil.UPDATA);
            KTVGroup ktvGroup = ktvGroupService.get(api.getId());

            if (ktvGroup == null) {
                ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "找不到对应协议");
                log_content.put(LogStringUtil.CAUSEFAIL,"找不到对应协议");
                log_sysLogApi.setStatus(LogStringUtil.FAILURE);
            } else if (!webUtilify.isAdmin(request) && (!ktvGroup.getUserid().equals(webUtilify.LoginUserinfo(request).getId()))) {
                //权限检查
                ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "找不到对应协议");
                log_content.put(LogStringUtil.CAUSEFAIL,"找不到对应协议");
                log_sysLogApi.setStatus(LogStringUtil.FAILURE);
            } else {
                log_content.put("修改前", JSON.toJSONString(ktvGroup));
                //数据插入
                ktvGroup.setName(api.getName());
                ktvGroup.setMemo(api.getMemo());
                log_content.put("修改后", JSON.toJSONString(api));
                ktvGroupService.updateT(ktvGroup, api.getKtvlist());
                ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "修改成功");
            }
        }
        log_sysLogApi.setContent(log_content);
        syslogService.WriteLog(log_sysLogApi);
        return JSON.toJSONString(ajaxCallback);
    }


    @RequestMapping(value = "/getprotocol")
    public String getprotocol(@RequestBody KTVGroup ktvGroup, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        //id验证
        if (Check.checkObjNull(ktvGroup.getId())) {
            ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "私有协议不存在");
            return JSON.toJSONString(ajaxCallback);
        }
        //数据库验证
        ktvGroup = ktvGroupService.get(ktvGroup.getId());
        if (Check.checkObjNull(ktvGroup)) {
            ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "私有协议不存在");
            return JSON.toJSONString(ajaxCallback);
        }
        //权限验证
        if (!webUtilify.isAdmin(request) && !ktvGroup.getUserid().equals(webUtilify.LoginUserinfo(request).getId())) {
            ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "私有协议不存在");
            return JSON.toJSONString(ajaxCallback);
        }
        UserKtv userKtv = new UserKtv();
        userKtv.setKtvgroupid(ktvGroup.getId());
        List<UserKtv> list = userKtvService.findByExampleEq(userKtv, "seq");
        List<UserKtvApi> li = userKtvdbtoapi(list);
        ProtocolListApi api = new ProtocolListApi(ktvGroup.getId(), ktvGroup.getName(), ktvGroup.getMemo(), li);
        ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "请求成功", api);
        return JSON.toJSONString(ajaxCallback);
    }


    private List ktvGroupdbtoapi(List<KTVGroup> list, Boolean isadmin) {
        List<ProtocolListApi> apis = new ArrayList<>();
        for (KTVGroup ktvGroup : list) {
            ProtocolListApi api = new ProtocolListApi(ktvGroup.getId(), ktvGroup.getName(), ktvGroup.getMemo(), ktvGroup.getUser().getName());
            if (!isadmin) {
                api.setUname(null);
            }
            apis.add(api);
        }
        return apis;
    }

    private List<UserKtvApi> userKtvdbtoapi(List<UserKtv> list) {
        List<UserKtvApi> apis2 = new ArrayList<>();
        for (UserKtv userKtv : list) {
            UserKtvApi api = new UserKtvApi(userKtv.getKtv().getId(), userKtv.getKtv().getName(),
                    userKtv.getKtv().getK(), userKtv.getTDescription(), userKtv.getSeq(), userKtv.getKtv().getTid());
            apis2.add(api);
        }
        return apis2;
    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody DelArr arr, HttpServletRequest request) {
        JSONObject log_content = new JSONObject();
        ktvGroupService.batchDelete(arr.getIds());
        log_content.put("id", JSON.toJSONString(arr.getIds()));
        syslogService.WriteLog(new SysLogApi(LogStringUtil.DEL, LogStringUtil.PRIVATEPROTOCOL, LogStringUtil.SUCCESS,
                log_content, webUtilify.LoginUserinfo(request).getId()));
        //id验证
        AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "请求成功");
        return JSON.toJSONString(ajaxCallback);
    }


}
